/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gen.service;

import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.gen.dao.GenDataBaseDictDao;
import com.thinkgem.jeesite.modules.gen.dao.GenTableColumnDao;
import com.thinkgem.jeesite.modules.gen.dao.GenTableDao;
import com.thinkgem.jeesite.modules.gen.dao.GenTableJoinColumnDao;
import com.thinkgem.jeesite.modules.gen.entity.GenTable;
import com.thinkgem.jeesite.modules.gen.entity.GenTableColumn;
import com.thinkgem.jeesite.modules.gen.entity.GenTableJoinColumn;
import com.thinkgem.jeesite.modules.gen.util.GenUtils;
import com.thinkgem.jeesite.modules.gen.util.TableColumnTableJoinColumnPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

/**
 * 业务表Service
 *
 * @author ThinkGem
 * @version 2013-10-15
 */
@Service
@Transactional(readOnly = false)
public class GenTableJoinColumnService extends BaseService {

    @Autowired
    private GenTableDao genTableDao;
    @Autowired
    private GenTableColumnDao genTableColumnDao;
    @Autowired
    private GenDataBaseDictDao genDataBaseDictDao;
    @Autowired
    private GenTableJoinColumnDao genTableJoinColumnDao;


    /**
     * 获取物理数据表列表
     *
     * @param genTableColumn
     * @return
     */
    public List<GenTableJoinColumn> getTableColumnFormDb(GenTableColumn genTableColumn) {
        // 如果有表名，则获取物理表
        if (StringUtils.isNotBlank(genTableColumn.getPrimaryTableName())) {
            // 添加新列
            GenTable genTable = new GenTable();
            genTable.setName(genTableColumn.getPrimaryTableName());
            List<GenTableColumn> columnList = genDataBaseDictDao.findTableColumnList(genTable);
            // 初始化列属性字段
            List<GenTableJoinColumn> result = GenUtils.initJoinColumnField(genTable, columnList);
            return result;
        }
        return null;
    }

    public boolean save(TableColumnTableJoinColumnPackage tableColumnTableJoinColumnPackage) {
        GenTableColumn genTableColumn = tableColumnTableJoinColumnPackage.getGenTableColumn();
        List<GenTableJoinColumn> columns = tableColumnTableJoinColumnPackage.getGenTableJoinColumns();
        long count = genTableColumnDao.updatePrimaryTable(genTableColumn);
        genTableColumn = genTableColumnDao.get(genTableColumn.getId());
        //删除旧的关联列
        genTableJoinColumnDao.deleteByTableColumnId(genTableColumn.getId());
        if (columns != null) {
            int i = 1;
            for (GenTableJoinColumn column : columns) {
                column.preInsert();
                column.setGenTableColumn(genTableColumn);
                column.setGenTable(genTableColumn.getGenTable());
                column.setSort(genTableColumn.getSort() + (i++));
                genTableJoinColumnDao.insert(column);
            }
        }
        return count > 0L;
    }

    public List<GenTableJoinColumn> getList(String genTableColumnId) {
        GenTableJoinColumn column = new GenTableJoinColumn();
        GenTableColumn genTableColumn = new GenTableColumn();
        genTableColumn.setId(genTableColumnId);
        column.setGenTableColumn(genTableColumn);
        List<GenTableJoinColumn> result = genTableJoinColumnDao.findList(column);
        if (result == null || result.isEmpty()) {
            return result;
        }
        Iterator<GenTableJoinColumn> iterator = result.iterator();
        while (iterator.hasNext()) {
            GenTableJoinColumn item = iterator.next();
            //System.out.println("GenTableJoinColumn:" + item);
        }
        return result;
    }
}
